/******************** 
	作用:添加幻灯片
	作者:蔡俊雄
	版本:V1.0
	时间:2015-08-20
********************/

$(function() {
	var maxCount=10;//最多能有多少个轮播图片
	//-----------------上传图片设置-----------------
	//编辑器设置
	var uploadEditor = KindEditor.editor({
//		uploadJson: '/Foundation/ImageTextReply?isUpfile=1',
		uploadJson : '/shop/admin/config/carousel/add?action=upLoadImg',
//		fileManagerJson : '/file/list',
		allowFileManager: false
	});
	//显示图片上传对话框
	function showImageUploadDialog(imageId, hiddenId) {
		uploadEditor.loadPlugin('image', function() {
			uploadEditor.plugin.imageDialog({
                            showRemote:false,
				imageUrl: $(hiddenId).val(),
				clickFn: function(url, title, width, height, border, align) {
					$(hiddenId).val(url);
					$(imageId).attr("src", url);
					uploadEditor.hideDialog();
				}
			});
		});
	}

	function initImageUploadDialog(btnId, imageId, hiddenId) {
		$(btnId).on("click", function(e) {
			showImageUploadDialog(imageId, hiddenId);
		});
	}

	$("#imageList").delegate("input[data-id=url]", "blur", function(e) {
		var url = $(this).val();
		if (url) {
			$(this).closest("li").find("img").attr("src", url);
		}
	});
	//-----------------上传图片-----------------
	//点击"上传"按钮
	$("#imageList").delegate("button[data-id=upload]", "click", function(e) {
		var imageId = $(this).closest("li").find("img:eq(0)");
		var hiddenId = $(this).parent().prev("input:eq(0)");
		showImageUploadDialog(imageId, hiddenId);
	});
	//-----------------删除-----------------
	//点击"删除"按钮
	$("#imageList").delegate("button[data-id=delete]", "click", function(e) {
		var li = $(this).closest("li");
		General.confirm("您确定要删除该图片吗?", null, startDelete, null, li);
	});
	//开始删除
	function startDelete(li) {
		li.remove();
	};
	//-----------------添加图片-----------------
	//点击"添加图片"按钮
	$("#add").on("click", function(e) {
		General.stopEvent(e);
		var count=$("#imageList>li").length;
		if(count>=maxCount){
			General.alert("最多只能添加"+maxCount+"张图片");
		}else{
			var str = $("#tpl-addImage").html();
			$("#imageList").append(str);
		}
	});
	//-----------------拖动图片排序-----------------
	var sort = new Sortable(document.getElementById("imageList"), {
		handle: ".carousel-image",
		draggable: "li"
	});

});